package com.yiguo.gmall_pulisher.controller;
import com.yiguo.gmall_pulisher.beans.DAUData;
import com.yiguo.gmall_pulisher.beans.GMVDate;
import com.yiguo.gmall_pulisher.service.PublisherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * http://localhost:8070/realtime-total?date=2020-08-15
 * [{"id":"dau","name":"新增日活","value":1200},
 * {"id":"new_mid","name":"新增设备","value":233}]
 *
 *
 * http://localhost:8070/realtime-hours?id=dau&date=2020-08-15
 * {"yesterday":{"11":383,"12":123,"17":88,"19":200 },
 * "today":{"12":38,"13":1233,"17":123,"19":688 }}
 */
@RestController
public class GmallPublishController {
    @Autowired
    private PublisherService publisherService;

    @RequestMapping(value = "/realtime-total")
    public Object handle1(String date){

        ArrayList<JSONObject> result = new ArrayList<>();

        Integer dau = publisherService.getDAUbyDate(date);
        Integer newMidCounts = publisherService.getnewMidIdCountByDate(date);
        Double gmv = publisherService.getGMVbyDate(date);

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("id","dau");
        jsonObject1.put("name","新增日活");
        jsonObject1.put("value",dau);


        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("id","new_mid");
        jsonObject2.put("name","新增设备");
        jsonObject2.put("value",newMidCounts);

        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("id","order_amount");
        jsonObject3.put("name","新增新增交易额");
        jsonObject3.put("value",gmv);


        result.add(jsonObject1);
        result.add(jsonObject2);
        result.add(jsonObject3);

        return result;
    }
    @RequestMapping(value = "/realtime-hours")
    public Object handle2(String id,String date){

        //根据今天求昨天的日期
        LocalDate toDay = LocalDate.parse(date);
        String yestodayDate = toDay.minusDays(1).toString();
        JSONObject result = new JSONObject();

        /**
         * 后续可能还会加需求
         */
        switch (id){
            case "dau" :
                List<DAUData> todayDatas = publisherService.getToDayDate(date);
                List<DAUData> yestodayDatas = publisherService.getToDayDate(yestodayDate);

                JSONObject jsonObject1 = parseData(todayDatas);
                JSONObject jsonObject2 = parseData(yestodayDatas);

                result.put("yesterday",jsonObject2);
                result.put("today",jsonObject1);
                break;
            default :
                List<GMVDate> todayDatasGMV = publisherService.getGMVdatasBydate(date);
                List<GMVDate> yestodayDatasGMV = publisherService.getGMVdatasBydate(yestodayDate);
                System.out.println(yestodayDatasGMV);
                JSONObject jsonObjectGMV = parseGMVData(todayDatasGMV);
                JSONObject jsonObjectGMV2 = parseGMVData(yestodayDatasGMV);

                result.put("yesterday",jsonObjectGMV2);
                System.out.println(("yesterday------" + jsonObjectGMV2.toString()));
                result.put("today",jsonObjectGMV);
                break;
        }

        return result;
    }




        //负责把List<DAUData>  封装成JSON 的方法parseData
    public JSONObject parseData(List<DAUData> datas){
        JSONObject jsonObject = new JSONObject();
        for (DAUData data : datas) {
            jsonObject.put(data.getHour(),data.getNum());
        }
        return jsonObject;
    }
    public JSONObject parseGMVData(List<GMVDate> datas){
        JSONObject jsonObject = new JSONObject();
        for (GMVDate data : datas) {
            System.out.println("parsegmv>>>>"+data);
            jsonObject.put(data.getHour(),data.getNum());
        }
        return jsonObject;
    }

}
